<%@ page import="java.util.Iterator" %>
<%@ page import="battleship.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html lang="et">
<head>
    <title>Laevade pommitamine</title>
    <%--<link rel="stylesheet" type="text/css" href="main.css" />--%>
    <%--<link rel="stylesheet" type="text/css" href="lobby.css" />--%>
    <%--<script type="text/javascript" src="jquery-1.7.1.js"></script>--%>
    <%--<script type="text/javascript" src="lobby.js"></script>--%>
    <%
        final String[] resources = {"main.css", "lobby.css", "min/jquery-1.7.1.js", "min/lobby.js"};
        ServletContext context = session.getServletContext();

        for (String resource : resources) {
            out.write(CacheFilter.getResource(context, resource));
        }
    %>
</head>
<body>
<%
	String userToken= request.getParameter("token");
	int id= (Integer) UserIdTokenHashMap.idTokenMap.get(userToken);
  //  int id = Integer.parseInt(request.getParameter("id"));
    User user = Context.users[id];
%>
<div id="content">
    <div id="title_area">
        <h1 id="title_header">Laevade pommitamine</h1>
    </div>
    <div id="center_area">
        <div id="lobby">
            <div class="lobbyTitle">
                <h2 id="lobbyTitleText">Tere, <%= user.getUsername() %>!</h2>
            </div>
            <div id="newGameStarted">
                <div class="newGameStartedText">
                    <p id="youStartedANewGame">Avasid ise uue mängu. Oota, kuni keegi liitub.</p>
                </div>
                <div id="cancelNewGameDiv">
                    <button id="cancelNewGameButton" class="button cancelNewGame">katkesta</button>
                </div>
            </div>
            <div id="lobbyButtons">
                <button id="logOutButton" class="button lobbyButton">Logi välja</button>
                <button id="gameHistoryButton" class="button lobbyButton">Mängude ajalugu</button>
                <button id="scoresButton" class="button lobbyButton">Edetabel</button>
                <button id="newGameButton" class="button lobbyButton">Ava uus mäng</button>
            </div>
            <div class="openGamesTitle">
                <p>Hetkel avatud mängud:</p>
            </div>
            <div class="currentGamesTitle">
                <p>Hetkel käimasolevad mängud:</p>
            </div>
            <div class="openGames">
                <div class="openGamesInner">
                    <form id="chooseOpponent" action="">
                        <%  Iterator<Game> openGamesIterator = Context.openGames.iterator();
                            while (openGamesIterator.hasNext()) {
                                Game game = openGamesIterator.next();
                                int opponentID; String opponentUsername;
                                if (game.player1ID == id) {
                                    opponentID = game.player2ID;
                                    opponentUsername = Context.users[opponentID].getUsername();
                                } else {
                                    opponentID = game.player1ID;
                                    opponentUsername = Context.users[opponentID].getUsername();
                                }
                        %>
                                <input type="radio" name="opponent" value="<%=opponentID%>" class="opponentRadio" title="a player"><span>  <%=opponentUsername%></span></input><br />
                        <%
                           }
                        %>
                    </form>
                </div>
            </div>
            <div class="currentGames">
                <div class="currentGamesInner">
                    <div id="currentGamesPairs">
                        <%  Iterator<Game> currentGamesIterator = Context.currentGames.iterator();
                            while (currentGamesIterator.hasNext()) {
                                Game game = currentGamesIterator.next();
                                String player1Username = Context.users[game.player1ID].getUsername();
                                String player2Username = Context.users[game.player2ID].getUsername();
                                String vsID = game.player1ID + "VS" + game.player2ID;
                                String vsUsername = player1Username + " VS " + player2Username;
                        %>
                        <p id=<%=vsID%>><%=vsUsername%></p>
                        <%
                            }
                        %>
                    </div>
                </div>
            </div>
            <div id="lobbyLowerArea">
                <% if (user.playing) { %>
                    <button id="continueGameBtn" class="button continueGameButton">Jätka mängu</button>
                <% } else { %>
                    <button id="joinGameBtn" class="button joinGameButton">Alusta</button>
                <% } %>
            </div>
        </div>
    </div>
    <div id="footer">
    </div>
</div>
</body>
</html>